package com.lightjet.macross.base.feign.api.util;

import com.lightjet.macross.base.feign.api.domain.AuthRole;
import lombok.extern.java.Log;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.List;

@Log
public class AuthUtil {

    public static boolean HasAnyRole(Collection<? extends GrantedAuthority> granted, String...roles)
    {
        boolean result = false;
        if(granted==null)
        {
            return result;
        }
        for(String role : roles)
        {
            if(granted.stream().anyMatch(e -> e.getAuthority().replaceFirst("ROLE_", "").toUpperCase().equals(role.toUpperCase())))
            {
                result = true;
                break;
            }
        }
        return result;
    }

    public static boolean HasAllRole(Collection<? extends GrantedAuthority> granted, String...roles)
    {
        boolean result = true;
        if(granted==null)
        {
            return result;
        }
        for(String role : roles)
        {
            if(granted.stream().noneMatch(e -> e.getAuthority().replaceFirst("ROLE_", "").toUpperCase().equals(role.toUpperCase())))
            {
                result = false;
                break;
            }
        }
        return result;
    }

}
